package cn.tedu.jsd2210.csmall.product.service.impl;

import cn.tedu.jsd2210.csmall.product.ex.ServiceException;
import cn.tedu.jsd2210.csmall.product.mapper.BrandMapper;
import cn.tedu.jsd2210.csmall.product.mapper.PictureMapper;
import cn.tedu.jsd2210.csmall.product.pojo.dto.BrandAddNewDto;
import cn.tedu.jsd2210.csmall.product.pojo.dto.BrandUpdateDto;
import cn.tedu.jsd2210.csmall.product.pojo.dto.PictureAddNewDto;
import cn.tedu.jsd2210.csmall.product.pojo.entity.Brand;
import cn.tedu.jsd2210.csmall.product.pojo.entity.Picture;
import cn.tedu.jsd2210.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.jsd2210.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.jsd2210.csmall.product.pojo.vo.PictureStandardVO;
import cn.tedu.jsd2210.csmall.product.service.IBrandService;
import cn.tedu.jsd2210.csmall.product.service.IPictureService;
import cn.tedu.jsd2210.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service

public class PictureServiceImpl implements IPictureService {
    @Autowired
    private PictureMapper pictureMapper;

    @Override
    public void addNew(PictureAddNewDto pictureAddNewDto) {
        log.debug("开始处理[增加图片]的业务,数据:{}",pictureAddNewDto);
        //图片以id为标识,地址和描述允许重复
        Picture picture=new Picture();
        //复制属性
        BeanUtils.copyProperties(pictureAddNewDto,picture);

        //调用mapper插入数据
        int rows = pictureMapper.insert(picture);
        if(rows!=1){
            String message="添加照片失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }


    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理[根据ID:{}删除图片]的业务",id);
        PictureStandardVO queryResult = pictureMapper.getStandardById(id);
        if(queryResult==null){
            String message="删除图片失败,尝试删除的图片数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        log.debug("开始执行[根据ID:{}删除图片]",id);
        int rows = pictureMapper.deleteById(id);
        if(rows!=1){
            String message="删除图片失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE,message);
        }
    }
}
